package jfox.platform.aaa.model;

import jfox.platform.aaa.entity.Role;
import jfox.platform.aaa.entity.User;
import jfox.platform.function.entity.Node;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author <a href="mailto:yang_y@sysnet.com.cn">Young Yang</a>
 */
public class UserContext implements Serializable {

    private User user;

    /**
     * 拥有的角色
     */
    private List<Role> roles = new ArrayList<Role>();

    /**
     * 拥有的功能列表
     */
    private List<Node> nodes = new ArrayList<Node>();

    public UserContext(User user, Collection<Role> roles, Collection<Node> nodes) {
        this.user = user;
        this.roles.addAll(roles);
        this.nodes.addAll(nodes);
    }

    public boolean hasPermission(long nodeId) {
        for (Node node : nodes) {
            if (node.getId() == nodeId) {
                return true;
            }
        }
        return false;
    }

    public boolean hasPermission(String nodeBindAction) {
        for (Node node : nodes) {
            if (node.getBindAction().equals(nodeBindAction)) {
                return true;
            }
        }
        return false;
    }

    public User getUser() {
        return user;
    }

    public String getUsername() {
        return getUser() != null ? getUser().getName() : "";
    }

    public String getUserFirstName() {
        return getUser() != null ? getUser().getFirstName() : "";
    }

    public String getUserLastName() {
        return getUser() != null ? getUser().getLastName() : "";
    }

    public long getUserId() {
        return getUser() != null ? getUser().getId() : 0;
    }

    public static void main(String[] args) {

    }
}
